package com.sgcc.sguap.bluefw;

import javax.sql.DataSource;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sgcc.sguap.bluefw.jit.domain.ProductLine;
import com.sgcc.sguap.bluefw.jit.repository.ProductLineDAO;
import com.sgcc.sguap.bluefw.jit.resource.FileResource;
import com.sgcc.sguap.bluefw.jit.resource.ProductLineResource;

import io.bluefw.blueui.BlueUIBundle;
import io.bluefw.microapp.auth.bundle.BlueAuthBundle;
import io.bluefw.microapp.auth.domain.Account;
import io.bluefw.microapp.auth.domain.Permission;
import io.bluefw.microapp.auth.domain.Role;
import io.bluefw.microapp.auth.repository.AccountDAO;
import io.bluefw.microapp.auth.resource.AccountResource;
import io.bluefw.microapp.auth.resource.LoginResource;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.forms.MultiPartBundle;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.migrations.MigrationsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class JITApplication extends Application<JITConfiguration> {
    public static void main(String[] args) throws Exception {
    	new JITApplication().run(args);
    }

    private final HibernateBundle<JITConfiguration> hibernateBundle =
            new HibernateBundle<JITConfiguration>(ProductLine.class, Account.class, Permission.class, Role.class) {
                @Override
                public DataSourceFactory getDataSourceFactory(JITConfiguration configuration) {
                    return configuration.getDataSourceFactory();
                }
            };

    @Override
    public String getName() {
        return "example";
    }
    private final BlueAuthBundle<JITConfiguration> authBundle = new BlueAuthBundle<JITConfiguration>() {
		@Override
		protected DataSource getDataSource(JITConfiguration configuration, Environment environment) {
			return configuration.getDataSourceFactory().build(environment.metrics(), "shiro");
		}
    };
    
    @Override
    public void initialize(Bootstrap<JITConfiguration> bootstrap) {
    	bootstrap.addBundle(new MultiPartBundle());
    	bootstrap.addBundle(new BlueUIBundle());
    	bootstrap.addBundle(authBundle);
    	bootstrap.addBundle(new AssetsBundle("/assets/src", "/", "index.html"));
    	
        // Enable variable substitution with environment variables
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(
                        bootstrap.getConfigurationSourceProvider(),
                        new EnvironmentVariableSubstitutor(false)
                )
        );

        bootstrap.addBundle(new MigrationsBundle<JITConfiguration>() {
            @Override
            public DataSourceFactory getDataSourceFactory(JITConfiguration configuration) {
                return configuration.getDataSourceFactory();
            }
        });
        bootstrap.addBundle(hibernateBundle);
    }

    @Override
    public void run(JITConfiguration configuration, Environment environment) {
    	environment.getObjectMapper().configure(
    			SerializationFeature.WRITE_ENUMS_USING_INDEX, true);
    	environment.getObjectMapper().configure(
    			DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    	
        environment.jersey().register(new ProductLineResource(
        		new ProductLineDAO(hibernateBundle.getSessionFactory())));
        environment.jersey().register(new AccountResource(
        		new AccountDAO(hibernateBundle.getSessionFactory())));
        environment.jersey().register(new LoginResource());

        
        
        environment.jersey().register(new FileResource());
    }
}
